import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
  state: {
  	hasLogin: false,
  	token:'',
		userInfo: {},
		store:{}
  },
  mutations: {
		// 设置用户
		setUserInfo(state, val){
			state.userInfo = val // token
			uni.setStorageSync('userInfo', val)
		},
  	//login 在用戶登后獲取服務器返回的數據進行賦值
  	login(state, data) {
			state.store = data.store
  		state.token = data.token.token // token
			state.userInfo = data.sysUser
  		state.hasLogin = true //修改登录状态， 表示已登录
  		// 将数据保存在本地 在下次打开app的时候判断是否有值， 如果有值则更新全局变量
  		uni.setStorageSync('token', data.token.token)
			uni.setStorageSync('store', data.store)
			uni.setStorageSync('userInfo', data.sysUser)
  	},
		setStore(state, storeInfo){
			state.store = storeInfo.store
			state.token = storeInfo.token.token
			uni.setStorageSync('store', storeInfo.store)
			uni.setStorageSync('token', storeInfo.token.token)
		},
  	// logout 用户退出登录后清除数据
  	logout(state){
  		state.hasLogin = false // 修改登录状态
  		state.token = ''
			state.userType = '',
			state.store = {}
			//同步清空本地缓存
  		uni.clearStorageSync('token') 
			uni.clearStorageSync('userInfo')
			uni.clearStorageSync('store')
  	}
  }
})
export default store
